﻿using BCCommon;
using BCCommon.Enums;
using BCEntity.IM.CustomService;
using Dapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace BCData.IM.CustomerService
{
    public class CustomerReceptionRecordData : ICustomerReceptionRecordData
    {
        private readonly IDatabaseContext databaseContext;

        public CustomerReceptionRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public Task<CustomerReceptionRecordEntity> GetCustomerReceptionRecordAsync(string businessType, string referenceNumber, long adminId, RoleType userType, long userId)
        {
            if (string.IsNullOrWhiteSpace(referenceNumber))
            {
                const string sql = @"SELECT * FROM customerreceptionrecord WHERE BusinessType = ?BusinessType AND ReferenceNumber IS NULL AND AdminId = ?AdminId AND UserType = ?UserType AND UserId = ?UserId";

                return this.databaseContext.Slave.QueryFirstOrDefaultAsync<CustomerReceptionRecordEntity>(sql, new { businessType, adminId, userType = userType.ToString(), userId });
            }
            else
            {
                const string sql = @"SELECT * FROM customerreceptionrecord WHERE BusinessType = ?BusinessType AND ReferenceNumber = ?ReferenceNumber AND AdminId = ?AdminId AND UserType = ?UserType AND UserId = ?UserId";

                return this.databaseContext.Slave.QueryFirstOrDefaultAsync<CustomerReceptionRecordEntity>(sql, new { businessType, referenceNumber, adminId, userType = userType.ToString(), userId });
            }
        }

        public Task<CustomerReceptionRecordEntity> InsertAsync(CustomerReceptionRecordEntity entity)
        {
            return this.databaseContext.Master.InsertAsync(entity, this.databaseContext.Transaction);
        }

        public Task<CustomerReceptionRecordEntity> UpdateAsync(CustomerReceptionRecordEntity entity)
        {
            return this.databaseContext.Master.UpdateAsync(entity, this.databaseContext.Transaction);
        }
    }
}
